/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.jSkyRoom.bridge.major;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import ru.jSkyRoom.bridge.IteratorBridge;
import ru.jSkyRoom.bridge.sub.LeveledEntryBridge;
import ru.jSkyRoom.finder.FinderByFormId;
import ru.jSkyRoom.interfaces.sub.JSkyLeveledEntry;
import ru.jSkyRoom.interfaces.major.Leveled;
import ru.jSkyRoom.interfaces.major.Major;
import skyproc.FormID;
import skyproc.LeveledEntry;
import skyproc.LeveledRecord;
import skyproc.MajorRecord;
import skyproc.exceptions.BadParameter;

/**
 *
 * @author Rakev
 */
public class LeveledBridge<E extends LeveledRecord> extends MajorBridge<E> implements Leveled {

    public LeveledBridge(E adapting) {
        super(adapting);
    }

    @Override
    public Collection<JSkyLeveledEntry> getEntries() {
        Collection<LeveledEntry> adaptingEntrys = getRawObject().getEntries();
        Collection<LeveledEntryBridge> entryBridges = getBridgeFactory().createBridges(adaptingEntrys, LeveledEntryBridge.class);

        return Collections.<JSkyLeveledEntry>unmodifiableCollection(entryBridges);
    }

    @Override
    public Collection<JSkyLeveledEntry> getRecursivelyEntries() {
        Collection<LeveledEntry> adaptingEntrys = getRawObject().getFlattenedEntries();
        Collection<LeveledEntryBridge> entryBridges = getBridgeFactory().createBridges(adaptingEntrys, LeveledEntryBridge.class);

        return Collections.<JSkyLeveledEntry>unmodifiableCollection(entryBridges);
    }

    @Override
    public void addEntry(JSkyLeveledEntry entry) {
        if (entry instanceof LeveledEntryBridge) {
            LeveledEntry rawEntry = ((LeveledEntryBridge) entry).getRawObject();
            getRawObject().addEntry(rawEntry);
        } else {
            throw new IllegalArgumentException("Only bridge supported.");
        }
    }

    @Override
    public void removeEntry(JSkyLeveledEntry entry) {
        if (entry instanceof LeveledEntryBridge) {
            LeveledEntry rawEntry = ((LeveledEntryBridge) entry).getRawObject();
            getRawObject().removeFirstEntry(rawEntry.getForm());
        } else {
            throw new IllegalArgumentException("Only bridge supported.");
        }
    }

    @Override
    public Iterator<JSkyLeveledEntry> iterator() {
        Iterator<LeveledEntry> rawIterator = getRawObject().iterator();
        return (Iterator) new IteratorBridge<LeveledEntry, LeveledEntryBridge>(rawIterator);
    }

    @Override
    public float getChanceNone() {
        return ((float) getRawObject().getChanceNone()) / 100;
    }

    @Override
    public void setChanceNone(float chance) {
        try {
            int chanceInt = (int) (chance * 100);
            getRawObject().setChanceNone(chanceInt);
        } catch (BadParameter ex) {
            throw new IllegalArgumentException("Chance none set outside range 0.0-1.0: " + chance);
        }
    }

    @Override
    public boolean getFlag(LeveledRecord.LVLFlag flag) {
        return getRawObject().get(flag);
    }

    @Override
    public void setFlag(LeveledRecord.LVLFlag flag) {
        getRawObject().set(flag, true);
    }

    @Override
    public void unsetFlag(LeveledRecord.LVLFlag flag) {
        getRawObject().set(flag, false);
    }
}
